#include <iostream>
#include "threadpool.h"
#include <chrono>
#include "Log.h"
#include <thread>
/*
有些场景，是希望获取线程执行任务的返回值

*/
class MyTask : public Task
{
public:
    MyTask(int begin, int end)
        : _begin(begin),
          _end(end) {}
        //   C++17出现了any类型,我们实现一下
    Any run()
    {
        // while (true)
        // {
        //     std::cout << "Hello World" << std::endl;
        //     std::this_thread::sleep_for(std::chrono::seconds(1));
        // }
        int sum = 0;
        int i = _begin;
        for(i;i<=_end;i++){
            sum+=i;
        }
        return sum;
    }

private:
    int _begin;
    int _end;
};

int main()
{
    ThreadPool tp;
    tp.start(4);
    // std::this_thread::sleep_for(std::chrono::seconds(5));
    Result res = tp.submitTask(std::make_shared<MyTask>());
    int sum = res.get()._cast<int>();

    
    tp.submitTask(std::make_shared<MyTask>());
    tp.submitTask(std::make_shared<MyTask>());
    tp.submitTask(std::make_shared<MyTask>());
    tp.submitTask(std::make_shared<MyTask>());
    return 0;
}